package cn.zyzpp.service.user.impl;

import cn.zyzpp.config.RolesConfig;
import cn.zyzpp.dao.UserDao;
import cn.zyzpp.entity.user.User;
import cn.zyzpp.service.user.UserService;
import cn.zyzpp.util.FileUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Create by yster@foxmail.com 2018/5/17/017 18:32
 */
@Service
@CacheConfig(cacheNames = {"UserService"})
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public boolean userSingIn(User user) {
        Md5Hash md5 = new Md5Hash(user.getPassword());
        user.setPassword(md5.toString());   //密码加密
        user.setUserRole(RolesConfig.USER);   //默认普通用户
        user.setDate(new Date());
        user.setImgUrl(FileUtil.filePath+"user.png");
        user.setOrUse(1);
        userDao.save(user);
        return true;
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public int getIdByUsername(String username) {
        return userDao.findIdByMail(username).getId();
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public String getPasswordByUsername(String username) {
        return userDao.findPasswordByMail(username).getPassword();
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")//“#参数名”或者“#p参数index”
    public Set<String> getRolesByUserName(String username) {
        Set<String> set = new HashSet<>();
        String role = userDao.findUserRoleByMail(username).getUserRole();
        set.add(role);
        return set;
    }

    @Override
    @Transactional
    @CacheEvict(allEntries = true)
    public void saveUserImg(String path, int id) {
        User user = userDao.findAllById(id);
        user.setImgUrl(path);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public User queryUserImg(int id) {
        return userDao.findImgUrlById(id);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public User queryAll(int id){
        return userDao.findAllById(id);
    }

    @Override
    @Cacheable(key = "targetClass + methodName +#p0")
    public Page<User> queryAll(int page, int limint) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, limint, sort);
        return userDao.findAll(pageable);
    }

    @Override
    @Transactional
    @CacheEvict(allEntries =true)
    public int updataOrUse(int id) {
        User user = userDao.findAllById(id);
        if (user.getOrUse() == 1){
            user.setOrUse(0);
            return 0;
        }else{
            user.setOrUse(1);
            return 1;
        }
    }

    @Override
    @Transactional
    public void updataPassWord(String mail, String password) {
        User user = userDao.findPasswordByMail(mail);
        user.setPassword(new Md5Hash(password).toString());
    }


}
